package com.wty.good.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.wty.commons.result.DataGrid;
import com.wty.commons.result.Result;
import com.wty.good.pojo.CommentWty;
import com.wty.good.service.CommentServiceWty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:wangtianyu
 * @Date：Created on 2020/5/6 0006 22:06
 */
@RestController
@RequestMapping("comment")
public class CommentControllerWty {

    @Autowired
    private CommentServiceWty commentService;

    /*新增评论*/
    @ApiOperation("新增评论")
    @PostMapping
    public Result<CommentWty> addComment(@RequestBody CommentWty comment){
        CommentWty commentData = commentService.addComment(comment);
        return Result.OK(commentData);
    }
    /*修改评论*/
    @ApiOperation("修改评论")
    @PutMapping("{_id}")
    public Result<CommentWty> updateComment(@PathVariable String _id,@RequestBody CommentWty comment){
        comment.set_id(_id);
        CommentWty commentData = commentService.updateComment(comment);
        return Result.OK(commentData);
    }
    /*删除评论*/
    @ApiOperation("删除评论")
    @DeleteMapping("{_id}")
    public Result<Boolean> deleteComment(@PathVariable String _id){
        Boolean flag = commentService.deleteCommentById(_id);
        return Result.OK(flag);
    }
    /*根据商品ID查询评论 分页*/
    @ApiOperation("根据商品ID查询评论 分页")
    @GetMapping("findCommentByIdAndPage/{goodId}/{current}/{size}")
    public Result<DataGrid<CommentWty>> findCommentByIdAndPage(@PathVariable Long goodId, @PathVariable Long current, @PathVariable Long size){
        DataGrid<CommentWty> dataGrid = commentService.findCommentByIdAndPage(goodId,current,size);
        return Result.OK(dataGrid);
    }
    /*点赞*/
    @ApiOperation("点赞")
    @PutMapping("thumbUp/{userId}/{_id}")
    public Result<Boolean> thumbUpAdd(@PathVariable Long userId, @PathVariable String _id){
        Boolean result = commentService.thumbUpAdd(userId,_id);
        if (result) {
            // 方法执行但是点赞失败
            return Result.CUSTOM(true,"不能重复点赞",false);
        }else {
            return Result.CUSTOM(true,"点赞成功",true);
        }
    }

    /*自定义查询接口*/
    @ApiOperation("自定义查询接口")
    @PostMapping("findByGoodIdAndContentContains")
    public Result<List<CommentWty>> findByGoodIdAndContentContains(@RequestBody CommentWty comment){
        List<CommentWty> comments = commentService.findByGoodIdAndContentContains(comment);
        return Result.OK(comments);
    }

}
